from typing import List


class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        # 使用单调栈
        st = [0]
        answer = [-1] * len(nums1)
        nums1_index_map = {n:i for i,n in enumerate(nums1)}
        for index2 in range(1, len(nums2)):
            while st and nums2[index2] > nums2[st[-1]]:
                if nums2[st[-1]] in nums1:
                    index1 = nums1_index_map[nums2[st[-1]]]
                    answer[index1] = nums2[index2]

                st.pop()
            st.append(index2)
        return answer
print(Solution().nextGreaterElement([1,3,5,2,4],[6,5,4,3,2,1,7]))